﻿/*
创建人：zhunan
日期：2022-05-09
描述：人员设备维护功能js
*/
var page = {
  pagetotal: 0,
  pageSize: 20,
  pageNumber: 1,
  pageType: getParam("type"), //type:1班次维护 2设备信息维护
  commonUrl: getRootPath() + "/Ashx/CommonHandler.ashx", //通用方法后台路径
  mainUrl: getRootPath() + "/Ashx/ApsHandler/EmployeeMaintPage.ashx", //本页面后台路径
  employeeResourceList: [],
  //初始化页面
  init: function () {
    page.bind();
    NewcustomizedEasyuiPagination('pp'); //定制化分页
    $('#pp').find('.pageSize').val(page.pageSize); //初始化行数
    page.initData(); //初始化页面数据
  },
  // 页面事件绑定
  bind: function () {
    // 人员信息表格
    $("#tbEmployeeMain").datagrid({
      columns: [[
        { field: 'ck', title: '选择', checkbox: true },
        { field: 'employeeid', title: '员工id', hidden: true },
        { field: 'employeename', title: '员工号', width: 100 },
        { field: 'fullname', title: '姓名', width: 80 },
        { field: 'cardnum', title: '卡号', width: 90 },
        { field: 'factoryname', title: '分厂', width: 100 },
        { field: 'workcentername', title: '工区', width: 100 },
        { field: 'teamname', title: '班组', width: 80 },
        { field: 'workshiftname', title: '班次', width: 80 },
        { field: 'workshiftid', title: '班次id', hidden: true },
        { field: 'rolenames', title: '角色', width: 100 },
        { field: 'makecalendarname', title: '日历', width: 100 },
        { field: 'skillnum', title: '技能', width: 60 },
        {
          field: 'isparallel', title: '一人多机', width: 60, formatter: function (value, row, index) {
            if (value === "1") {
              return "是";
            } else {
              return "";
            }
          }
        },
        {
          field: 'isschedule', title: '参与排班', width: 60, formatter: function (value, row, index) {
            if (value === "1") {
              return "是";
            } else {
              return "";
            }
          }
        },
        { field: 'makecalendarid', title: '日历id', hidden: true }
      ]],
      onLoadSuccess: function (data) {
        $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
          total: page.pagetotal,
          pageSize: page.pageSize,
          pageNumber: page.pageNumber
        }); 
        NewcustomizedEasyuiPagination('pp')
      },
      // 选择此行
      onCheck: function (index, rowData) {
        page.reset(false, false, true);//add:Wangjh 20220728
        page.getEmployeeDetails($(this).datagrid("getChecked"));//add:Wangjh 20220728
        leaveInfo.pageNumber = 1;
        leaveInfo.GetEmployeeLeaveInfo();// add zhunan 2023.2.17
      },
      // 取消选择此行
      onUncheck: function (index, rowData) {
        page.reset(false, false, true);//add:Wangjh 20220728
        page.getEmployeeDetails($(this).datagrid("getChecked"));//add:Wangjh 20220728
      },
      // 选择所有行
      onCheckAll: function (rowData) {
        page.reset(false, false, true);//add:Wangjh 20220728
        page.getEmployeeDetails(rowData);//add:Wangjh 20220728
      },
      // 取消选择所有行
      onUncheckAll: function (rowData) {
        page.reset(false, false, true);//add:Wangjh 20220728
      }
    });

    // 人员设备表格
    $("#tbEmployeeResources").datagrid({
      columns: [[
        { field: 'ck', title: '选择', checkbox: true },
        { field: 'employeeid', title: '员工id', hidden: true },
        { field: 'fullname', title: '姓名', width: 80 },
        { field: 'employeename', title: '员工号', width: 80 },
        { field: 'resourceid', title: '设备id', hidden: true },
        { field: 'resourcename', title: '设备编码', width: 150 },
        { field: 'description', title: '设备名称',width: 120 },
        { field: 'resourcefamilyname', title: '设备型号', hidden: true }
      ]]
    });

    // 设备列表表格
    $("#tbResourceList").datagrid({
      columns: [[
        { field: 'ck', title: '选择', checkbox: true },
        { field: 'resourcefamilyname', title: '设备型号', width: 80 },
        { field: 'resourcename', title: '设备编码', width: 175 },
        { field: 'description', title: '设备名称', width: 130 },
        { field: 'resourceid', title: '设备id', hidden: true }
      ]]
    });

    //分页事件
    $('#pp').pagination({
      onSelectPage: function (pageNumber, pageSize) {
        NewcustomizedEasyuiPagination('pp'); //定制化分页
        page.pageSize = pageSize; page.pageNumber = pageNumber;
        page.reset(false, true, true);
        page.search();
      }
    });

    //绑定分页go按钮
    $(function () {
      $('#pageGo').on("click", function () {
         var pageNumber = Number($(this).parents('tr').find('.pagination-num').val());
        page.pageSize = $('#pp').find('.pageSize').val() == "" ? page.pageSize : Number($('#pp').find('.pageSize').val());
        page.pageNumber = pageNumber;
        page.search();
      });
    });
    // 查询人员信息
    $("#btnSearch").on("click", function () {
      page.employeeResourceList = [];
      $("#tbEmployeeResources").datagrid('loadData', page.employeeResourceList);
      page.pageNumber = 1;
      page.reset(false, true, true);
      page.search();
    });

    // 重置
    $("#btnReset").on('click', function () {
      page.reset(true, true, true);
    })

    // 更新人员班次、日历、技能等信息
    $("#btnUpdateEmployeeInfo").on('click', function () {
      var datas = $("#tbEmployeeMain").datagrid('getChecked');
      if (datas.length == 0) {
        displayMessage("请在上方区域勾选待保存的人员！", false);
        return;
      }
      var workshiftid = $('#ddlWorkShift').combobox('getValue');
      var makecalendarid = $('#ddlCalender').combobox('getValue');
      var skillnum = $('#ddlSkill').combobox('getValue');
      datas.forEach(function (item) {
        item.workshiftid = workshiftid;
        item.makecalendarid = makecalendarid;
        item.skillnum = skillnum;
        item.isParallel = $('#ddlIsParallel').combobox('getValue');
        item.isSchedule = $('#ddlIsSchedule').combobox('getValue');
      })

      $.ajax({
        type: "post",
        url: page.mainUrl + "?actions=updateEmployeeInfo",
        data: { action: "UpdateEmployeeInfo", postData: JSON.stringify(datas) },
        cashe: false,
        dataType: 'json',
        success: function (res) {
          if (res.Result) {
            displayMessage("班次信息保存成功!", true);
            var datas = $("#tbEmployeeMain").datagrid('getChecked'); //获取所有勾选的行
            var newData = {};
            var workshiftid = $('#ddlWorkShift').combobox('getValue');
            var workshiftname = $('#ddlWorkShift').combobox('getText');
            var makecalendarid = $('#ddlCalender').combobox('getValue');
            var makecalendarname = $('#ddlCalender').combobox('getText');
            var skillnum = $('#ddlSkill').combobox('getValue');
            newData.workshiftname = workshiftname;
            newData.workshiftid = workshiftid;
            newData.makecalendarid = makecalendarid;
            newData.makecalendarname = makecalendarname;
            newData.skillnum = skillnum;
            for (var i = 0; i < datas.length; i++) {
              var index = $("#tbEmployeeMain").datagrid('getRowIndex', datas[i]);
              var row = $.extend({}, datas[i], newData);
              $("#tbEmployeeMain").datagrid('updateRow', { index: index, row: row });
              $("#tbEmployeeMain").datagrid('acceptChanges');
              $("#tbEmployeeMain").datagrid('refreshRow', index);
            }
          } else {
            displayMessage(res.Message, res.Result);
          }
        }
      });
    })

    // 查询设备列表
    $("#btnResourceSearch").on('click', function () {
      clearMessage();
      var formData = {
        action: "GetResourceList",
        // resourcefamilyid: $('#ddlResourceFamily').combobox('getValue'),
        resourcename: $('#txtResourceName').textbox('getValue'),
        description: $('#txtDescription').textbox('getValue'),
        factoryid: $('#ddlResourceFactory').textbox('getValue')
      };

      $.ajax({
        type: "Post",
        url: page.mainUrl + '?action=GetResourceList',
        data: formData,
        cashe: false,
        async: true,
        beforeSend: function () {
          $('#tbResourceList').datagrid('loading');
          $(this).attr("disabled", "disabled"); //禁用按钮
        },
        success: function (data) {
          $('#tbResourceList').datagrid('loaded');
          $(this).removeAttr("disabled"); //恢复按钮
          var result = JSON.parse(data);
          if (result.Result) {
            $('#tbResourceList').datagrid('loadData', { rows: result.DBTable });
          } else {
            displayMessage(result.Message, false);
          }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          $('#tbResourceList').datagrid('loaded');
          $(this).removeAttr("disabled"); //恢复按钮
          alert(XMLHttpRequest.responseText);
        }
      });
    })

    // 人员添加设备
    $("#addResource").on('click', function () {
      clearMessage()
      var datas = $("#tbResourceList").datagrid('getChecked');
      if (datas.length == 0) {
        displayMessage("请在右侧区域勾选待添加的设备", false);
        return;
      }

      var ckEmployees = $("#tbEmployeeMain").datagrid('getChecked');
      if (ckEmployees.length == 0) {
        displayMessage("请在上方区域勾选要添加设备的人员", false);
        return;
      }

      ckEmployees.forEach(function (e) {
        datas.forEach(function (r) {
          var data = {
            employeeid: e.employeeid,
            fullname: e.fullname,
            employeename: e.employeename,
            resourceid: r.resourceid,
            resourcename: r.resourcename
          }

          var isExist = page.employeeResourceList.some(function (i) {
            return i.employeeid === data.employeeid && i.resourceid === data.resourceid
          })

          if (!isExist) {
            page.employeeResourceList.push(data)
          }
        })
      });
      $("#tbEmployeeResources").datagrid('loadData', page.employeeResourceList);

      $.ajax({
        type: "post",
        url: page.mainUrl + "?actions=saveEmployeeResource",
        data: { action: "SaveEmployeeResource", postData: JSON.stringify(page.employeeResourceList) },
        cashe: false,
        dataType: 'json',
        success: function (res) {
          if (res.Result) {
            displayMessage("设备添加成功!", true);
          } else {
            displayMessage(res.Message, res.Result);
          }
        }
      });
    });

    // 人员移除设备
    $("#removeResource").on('click', function () {
      clearMessage()
      var ckEmployees = $("#tbEmployeeMain").datagrid('getChecked');
      if (ckEmployees.length == 0) {
        displayMessage("请在上方区域勾选要移除设备的人员", false);
        return;
      }

      var datas = $("#tbEmployeeResources").datagrid('getChecked');
      if (datas.length == 0) {
        displayMessage("请在左侧区域勾选待移除的设备", false);
        return;
      }

      for (var i = 0; i < datas.length; i++) {
        var index = $("#tbEmployeeResources").datagrid('getRowIndex', datas[i]);
        page.employeeResourceList.splice(index, 1)
      }

      $("#tbEmployeeResources").datagrid('loadData', page.employeeResourceList);

      var willBeRemovedResourceList = page.employeeResourceList;

      if (willBeRemovedResourceList.length == 0) {
        ckEmployees.forEach(function (item) {
          willBeRemovedResourceList.push({
            employeeid: item.employeeid,
            fullname: item.fullname,
            employeename: item.employeename,
            resourceid: "",
            resourcename: "",
            description: "",
            resourcefamilyname: ''
          })
        })
      }

      $.ajax({
        type: "post",
        url: page.mainUrl + "?actions=saveEmployeeResource",
        data: { action: "SaveEmployeeResource", postData: JSON.stringify(willBeRemovedResourceList) },
        cashe: false,
        dataType: 'json',
        success: function (res) {
          if (res.Result) {
            displayMessage("设备移除成功!", true);
            page.employeeResourceList = res.DBTable;
          } else {
            displayMessage(res.Message, res.Result);
          }
        }
      });
    });
  },
  //初始化页面
  initData: function () {
    page.getFactory();
    page.getRole();
    page.getWorkshift();
    page.getCanlender();
    page.getResourceFamily();
    $("#workShiftZone").hide();
    $("#resourceZone").hide();
    var typearr = page.pageType.split(',');
    if (page.pageVaild(typearr)) {
      for (var i = 0; i < typearr.length; i++) {
        switch (typearr[i]) {
          case "1":
            $("#workShiftZone").show();
            break;
          case "2":
            $("#resourceZone").show();
            break;
          default:
            break;
        }
      }
    }
  },
  //url参数验证
  pageVaild: function (arr) {
    if (arr.length === 0) {
      displayMessage("未指定功能类型", false);
      return false;
    }
    var isvaild = true;
    var tarr = ["1", "2", "3", "4"];
    for (var i = 0; i < arr.length; i++) {
      if ($.inArray(arr[i], tarr) === -1) {
        displayMessage("参数出错", false);
        isvaild = false;
        break;
      }
    }
    return isvaild;
  },
  // 查询
  search: function () {
    var formData = {
      action: "GetEmployee",
      pageSize: page.pageSize,
      pageNumber: page.pageNumber,
      rolename: $('#ddlRole').combobox('getValue'),
      factoryid: $('#ddlFactory').combobox('getValue'),
      workcenterid: $('#ddlWorkcenter').combobox('getValue'),
      teamid: $('#ddlTeam').combobox('getValue'),
      fullname: $('#txtFullname').textbox('getValue'),
      employeename: $('#txtEmployeename').textbox('getValue')
    };

    $.ajax({
      type: "Post",
      url: page.mainUrl + "?actions=getEmployeeInfo",
      data: formData,
      cashe: false,
      async: true,
      beforeSend: function () {
        $('#tbEmployeeMain').datagrid('loading');
        $(this).attr("disabled", "disabled"); //禁用按钮
      },
      success: function (data) {
        $('#tbEmployeeMain').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        var result = JSON.parse(data);
        if (result.Result) {
          //给分页对象赋值
          page.pagetotal = result.RowCount;
          $('#tbEmployeeMain').datagrid('loadData', { total: result.RowCount, rows: result.DBTable });
        } else {
          displayMessage(result.Message, false);
        }
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        $('#tbEmployeeMain').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        alert(XMLHttpRequest.responseText);
      }
    });
  },
  // 清除方法，重要！！！
  reset: function (iscondition, isresult, detail) {
    clearMessage();
    if (iscondition) {
      //add:Wangjh 20220728
      $('#ddlRole').combobox('select', '');
      $('#ddlFactory').combobox('select', '');
      $('#ddlWorkcenter').combobox('select', '');
      $('#ddlTeam').combobox('select', '');

      $('#txtFullname').textbox('clear');
      $('#txtEmployeename').textbox('clear');
    }
    if (isresult) {
      clearEasyuiDataGrid("tbEmployeeMain", 1);
    }
    if (detail) {
      page.employeeResourceList = [];
      clearEasyuiDataGrid("tbEmployeeResources", 1);
      clearEasyuiDataGrid("tbResourceList", 1);

      //add:Wangjh 清除基本信息
      $('#ddlWorkShift').combobox('select', '');
      $('#ddlCalender').combobox('select', '');
      $('#ddlSkill').combobox('select', '');
      $('#ddlIsParallel').combobox('select', '');
    }
  },
  // 获取角色列表
  getRole: function () {
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=getRoleList",
      data: { action: "GetRole" },
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          res.DBTable.unshift({ rolename: '', rolename: '' });
          $("#ddlRole").combobox({
            valueField: 'rolename',
            textField: 'rolename',
            panelHeight: 200,
            data: res.DBTable, //下拉数据
          });
          $("#ddlRole").combobox('select', '');
        } else {
          displayMessage(res.Message, res.Result);
        }
      }
    });
  },
  // 获取分厂列表
  getFactory: function () {
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=getFactory",
      data: { action: "GetFactory" },
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          //绑定分厂
          res.DBTable.unshift({ factoryid: '', factoryname: '' });
          $("#ddlFactory").combobox({
            valueField: 'factoryid',
            textField: 'factoryname',
            panelHeight: 200,
            data: res.DBTable, //下拉数据
            onSelect: function (record) {
              if (record.factoryid) {
                page.bindWorkcenter(record.factoryid);// 加载工区
                // 清空班组
                $('#ddlTeam').combobox('clear');
                $('#ddlTeam').combobox('loadData', []);
              }
            }
          });
          $("#ddlResourceFactory").combobox({
            valueField: 'factoryid',
            textField: 'factoryname',
            panelHeight: 200,
            data: res.DBTable, //下拉数据
            onSelect: function (record) { }
          });
          $("#ddlFactory").combobox('select', '');
          $("#ddlResourceFactory").combobox('select', '');
        } else {
          displayMessage(res.Message, res.Result);
        }
      }
    });
  },
  // 绑定工区
  bindWorkcenter: function (factoryid) {
    $.ajax({
      url: page.mainUrl + "?actions=getWorkcenterList",
      data: { action: "GetWorkcenter", factoryid: factoryid },
      type: "post",
      cashe: false,
      dataType: 'json',
      success: function (res) { //ajax成功后
        if (res.Result) {
          //绑定工区
          res.DBTable.unshift({ workcenterid: '', workcentername: '' });
          $("#ddlWorkcenter").combobox({
            valueField: 'workcenterid',
            textField: 'workcentername',
            panelHeight: 200,
            data: res.DBTable, //下拉数据
            onSelect: function (record) {
              if (record.workcenterid) {
                page.bindTeam(record.workcenterid); //加载班组
              }
            }
          });
          $("#ddlWorkcenter").combobox('select', '');
        } else {
          displayMessage(res.Message, res.Result);
        }
      }
    });
  },
  // 绑定班组
  bindTeam: function (workcenterid) {
    $.ajax({
      url: page.mainUrl + "?actions=getTeamList",
      data: { action: "GetTeam", workcenterid: workcenterid },
      type: "post",
      cashe: false,
      dataType: 'json',
      success: function (res) { //ajax成功后
        if (res.Result) {
          //绑定班组
          res.DBTable.unshift({ teamid: '', teamname: '' });
          $("#ddlTeam").combobox({
            valueField: 'teamid',
            textField: 'teamname',
            panelHeight: 200,
            data: res.DBTable, //下拉数据
          });
          $("#ddlTeam").combobox('select', '');
        } else {
          displayMessage(res.Message, res.Result);
        }
      }
    });
  },
  // 获取班次信息
  getWorkshift: function () {
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=getWorkshiftList",
      data: { action: "GetWorkshift" },
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          res.DBTable.unshift({ workshiftid: '', workshiftname: '' });
          $("#ddlWorkShift").combobox({
            valueField: 'workshiftid',
            textField: 'workshiftname',
            panelHeight: 200,
            data: res.DBTable
          });
          $("#ddlWorkShift").combobox('select', '');
        } else {
          displayMessage(res.Message, res.Result);
        }
      }
    });
  },
  // 获取日历信息
  getCanlender: function () {
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=getCanlender",
      data: { action: "GetCalender" },
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          res.DBTable.unshift({ makecalendarid: '', makecalendarname: '' });
          $("#ddlCalender").combobox({
            valueField: 'makecalendarid',
            textField: 'makecalendarname',
            panelHeight: 200,
            data: res.DBTable
          });
          $("#ddlCalender").combobox('select', '')
        } else {
          displayMessage(res.Message, res.Result);
        }
      }
    });
  },
  // 获取设备类型型号
  getResourceFamily: function () {
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=getResourceFamily",
      data: { action: "GetResourceFamily" },
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          res.DBTable.unshift({ makecalendarid: '', makecalendarname: '' });
          $("#ddlResourceFamily").combobox({
            valueField: 'resourcefamilyid',
            textField: 'resourcefamilyname',
            panelHeight: 200,
            data: res.DBTable
          });
          $("#ddlResourceFamily").combobox('select', '')
        } else {
          displayMessage(res.Message, res.Result);
        }
      }
    });
  },
  // 查询用户已有设备
  getEmployeeResources: function (data) {
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=GetEmployeeResources",
      data: { action: "GetEmployeeResources", postData: JSON.stringify(data) },
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          res.DBTable.forEach(function (item) {
            page.employeeResourceList.push(item);
          });
          $("#tbEmployeeResources").datagrid('loadData', page.employeeResourceList)
        }
      }
    });
  },
  //查询详细信息 add:wangjh 20220728
  getEmployeeDetails: function (data) {
    page.getEmployeeResources(data);
    if (data.length == 1) {
      $('#ddlWorkShift').combobox('select', data[0].workshiftid);
      $('#ddlCalender').combobox('select', data[0].makecalendarid);
      $('#ddlSkill').combobox('select', data[0].skillnum);
      $('#ddlIsParallel').combobox('select', data[0].isparallel);
    }
  }
};

var leaveInfo = {
  pagetotal: 0,
  pageSize: 5,
  pageNumber: 1,
  employeeid: '',
  init: function () {
    leaveInfo.bind();
  },
  bind: function () {
    // 休假信息表格
    $("#tbEmployeeLeaveInfo").datagrid({
      columns: [[
        { field: 'ck', title: '选择', checkbox: true },
        { field: 'employeeid', title: '员工id', hidden: true },
        { field: 'employeeleaveinfoid', title: 'employeeleaveinfoid', hidden: true },
        { field: 'employeename', title: '员工号', width: 80 },
        { field: 'fullname', title: '姓名', width: 80 },
        { field: 'cause', title: '休假事由', width: 200 },
        {
          field: 'leavestartdate', title: '休假开始时间', width: 150,
          formatter: function (value) {
            return value.replace('T', ' ');
          }
        },
        {
          field: 'leaveenddate', title: '休假结束时间', width: 150,
          formatter: function (value) {
            return value.replace('T', ' ');
          }
        },
        { field: 'creater', title: '创建人', width: 80 },
        {
          field: 'createdate', title: '创建时间', width: 150,
          formatter: function (value) {
            return value.replace('T', ' ');
          }
        }
      ]],
      onLoadSuccess: function (data) {
        $('#pp1').pagination('refresh', {	// 改变选项，并刷新分页栏信息
          total: leaveInfo.pagetotal,
          pageSize: leaveInfo.pageSize,
          pageNumber: leaveInfo.pageNumber
        });
        NewcustomizedEasyuiPagination('pp1'); //定制化分页
      }
    });

    // 绑定分页
    $('#pp1').pagination({
      onSelectPage: function (pageNumber, pageSize) {
        NewcustomizedEasyuiPagination('pp1'); //定制化分页
        leaveInfo.pageSize = pageSize;
        leaveInfo.pageNumber = pageNumber;
        leaveInfo.GetEmployeeLeaveInfo();
      }
    });

    // 添加按钮
    $("#btnLeaveInfoAdd").on('click', function () {
      $("#leaveInfoMsg").html("");
      var ckEmployees = $("#tbEmployeeMain").datagrid('getChecked');
      if (ckEmployees.length == 0) {
        leaveInfo.showLeaveInfoMaintMsg('请选择人员', false);
        return;
      }
      if (ckEmployees.length > 1) {
        leaveInfo.showLeaveInfoMaintMsg('一次只能选择1人', false);
        return;
      }
      var leaveReason = $("#txtLeaveReason").textbox('getValue');
      if (isNullOrWhiteSpaceStr(leaveReason)) {
        leaveInfo.showLeaveInfoMaintMsg('休假事由不能为空', false);
        return;
      }
      var startTime = $("#txtLeaveStartDate").datetimebox("getValue");
      if (isNullOrWhiteSpaceStr(startTime)) {
        leaveInfo.showLeaveInfoMaintMsg('请选择休假开始时间', false);
        return;
      }
      var endTime = $("#txtLeaveEndDate").datetimebox("getValue");
      if (isNullOrWhiteSpaceStr(endTime)) {
        leaveInfo.showLeaveInfoMaintMsg('请选择休假结束时间', false);
        return;
      }
      if (startTime >= endTime) {
        leaveInfo.showLeaveInfoMaintMsg('休假结束时间不能早于休假开始时间', false);
        return;
      }
      var postData = {
        action: 'SaveEmployeeLeaveInfo',
        cause: leaveReason,//事由
        employeeid: ckEmployees[0].employeeid,//休假人
        leaveenddate: endTime,//休假开始时间
        leavestartdate: startTime //休假结束时间
      };
      leaveInfo.SaveEmployeeLeave(postData);
    });

    // 删除按钮
    $("#btnLeaveInfoDele").on('click', function () {
      var ckData = $("#tbEmployeeLeaveInfo").datagrid('getChecked');
      if (ckData.length == 0) {
        leaveInfo.showLeaveInfoMaintMsg('请至少选择一条待删记录', false);
        return;
      }
      $.messager.confirm('删除前确认', '你确定要删除吗?', function (r) {
        // 确认删除
        if (r) {
          leaveInfo.DeleteEmployeeLeaveInfo(ckData);
        }
      });
    });
  },
  // 查询人员请假信息
  GetEmployeeLeaveInfo: function () {
    var employeeid = $("#tbEmployeeMain").datagrid('getChecked')[0].employeeid;
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=GetEmployeeLeaveInfo",
      data: {
        action: 'GetEmployeeLeaveInfo',
        pageSize: leaveInfo.pageSize,
        pageNumber: leaveInfo.pageNumber,
        employeeid: employeeid
      },
      cashe: false,
      beforeSend: function () {
        $('#tbEmployeeLeaveInfo').datagrid('loading');
        $(this).attr("disabled", "disabled"); //禁用按钮
      },
      success: function (data) {
        $('#tbEmployeeLeaveInfo').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        var result = JSON.parse(data);
        if (result.Result) {
          //给分页对象赋值
          leaveInfo.pagetotal = result.RowCount;
          $('#tbEmployeeLeaveInfo').datagrid('loadData', { total: result.RowCount, rows: result.DBTable });
        } else {
          leaveInfo.showLeaveInfoMaintMsg(result.Message, false);
        }
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        $('#tbEmployeeLeaveInfo').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        alert(XMLHttpRequest.responseText);
      }
    });
  },
  // 增加人员休假信息
  SaveEmployeeLeave: function (postData) {
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=SaveEmployeeLeaveInfo",
      data: postData,
      cashe: false,
      dataType: 'json',
      success: function (res) {
        leaveInfo.showLeaveInfoMaintMsg(res.Message, res.Result);
        if (res.Result) {
          $("#txtLeaveReason").textbox('setValue', "");
          $("#txtLeaveStartDate").datetimebox("setValue", "");
          $("#txtLeaveEndDate").datetimebox("setValue", "");
          leaveInfo.GetEmployeeLeaveInfo();
        }
      }
    });
  },
  // 删除人员休假信息
  DeleteEmployeeLeaveInfo: function (deleteData) {
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=DeleteEmployeeLeaveInfo",
      data: { action: 'DeleteEmployeeLeaveInfo', postData: JSON.stringify(deleteData) },
      cashe: false,
      dataType: 'json',
      success: function (res) {
        leaveInfo.showLeaveInfoMaintMsg(res.Message, res.Result);
        if (res.Result) {
          leaveInfo.pageNumber = 1;
          leaveInfo.GetEmployeeLeaveInfo();
        }
      }
    });
  },
  showLeaveInfoMaintMsg: function (strMsg, isSuccess) {
    if (isSuccess) {
      $("#leaveInfoMsg").html("<span>消息：" + strMsg + "<span>");
    } else {
      $("#leaveInfoMsg").html("<span style='color:red;'>消息：" + strMsg + "<span>");
    }
  }
};
$(function(){    
    page.init();
    leaveInfo.init();
});